package com.mall.dao;

import com.mall.domain.OrderCopy;
import com.mall.domain.OrderDetails;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface IOrderDao {

	@Results(id = "orderMap",
			value = {
					@Result(property = "orderCode", column = "order_code"),
					@Result(property = "paymentMoney", column = "payment_money"),
					@Result(property = "paymentType", column = "payment_type"),
					@Result(property = "postFee", column = "post_fee"),
					@Result(property = "paymentTime", column = "payment_time"),
					@Result(property = "consignTime", column = "consign_time"),
					@Result(property = "endTime", column = "end_time"),
					@Result(property = "closeTime", column = "close_time"),
					@Result(property = "shippingName", column = "shipping_name"),
					@Result(property = "shippingCode", column = "shipping_code"),
					@Result(property = "memberId", column = "member_id"),
					@Result(property = "buyerMsg", column = "buyer_msg"),
					@Result(property = "buyerNickname", column = "buyer_nickname"),
					@Result(property = "buyerComment", column = "buyer_comment")
			})
	@Select("select * from tb_order where flag=0 and status=#{status}")
	List<OrderCopy> findAllOrderByType(@Param("status") Integer type);

	@Results(
			value = {
					@Result(property = "orderCode", column = "order_code"),
					@Result(property = "paymentMoney", column = "payment_money"),
					@Result(property = "paymentType", column = "payment_type"),
					@Result(property = "postFee", column = "post_fee"),
					@Result(property = "paymentTime", column = "payment_time"),
					@Result(property = "consignTime", column = "consign_time"),
					@Result(property = "endTime", column = "end_time"),
					@Result(property = "closeTime", column = "close_time"),
					@Result(property = "shippingName", column = "shipping_name"),
					@Result(property = "shippingCode", column = "shipping_code"),
					@Result(property = "memberId", column = "member_id"),
					@Result(property = "buyerMsg", column = "buyer_msg"),
					@Result(property = "buyerNickname", column = "buyer_nickname"),
					@Result(property = "buyerComment", column = "buyer_comment")
			})
	@Select("select * from tb_order where flag=0 and order_code=#{order_code}")
	List<OrderCopy> findOrderById(@Param("order_code") String order_code);

	//删除订单 :将订单的flag设为1，默认是0
	@Update("UPDATE tb_order SET flag=1 WHERE order_code=#{order_code}")
	Integer deleteOrder(@Param("order_code") String order_code);

    //支付订单
	@Update("update tb_order SET payment_time = #{payment_time},STATUS=3 where order_code=#{order_code}")
	Integer payOrder(@Param("payment_time") Date payment_time, @Param("order_code") String order_code);

	// 确认收货
	@Update("update tb_order SET end_time = #{end_time},STATUS=1 where order_code=#{order_code}")
	Integer endOrder(@Param("end_time") Date end_time, @Param("order_code") String order_code);

	//查询全部订单
	@ResultMap("orderMap")
	@Select("select * from tb_order where flag=0")
	List<OrderCopy> findAllOrder();

	@Results(id="orderDetails",value = {
			@Result(property = "orderCode", column = "order_code"),
			@Result(property = "paymentMoney", column = "payment_money"),
			@Result(property = "paymentType", column = "payment_type"),
			@Result(property = "postFee", column = "post_fee"),
			@Result(property = "paymentTime", column = "payment_time"),
			@Result(property = "consignTime", column = "consign_time"),
			@Result(property = "endTime", column = "end_time"),
			@Result(property = "closeTime", column = "close_time"),
			@Result(property = "shippingName", column = "shipping_name"),
			@Result(property = "shippingCode", column = "shipping_code"),
			@Result(property = "memberId", column = "member_id"),
			@Result(property = "buyerMsg", column = "buyer_msg"),
			@Result(property = "buyerNickname", column = "buyer_nickname"),
			@Result(property = "buyerComment", column = "buyer_comment"),
			@Result(property = "orderDetails",column = "order_code",many = @Many(select = "com.mall.dao.IOrderDetailsDao.findOrderDetailsById",fetchType = FetchType.LAZY))
	})
	@Select("select * from tb_order where flag=0")
	List<OrderCopy> findAllOrderDetails();



}
